package com.atguigu.tingshu.payment.service.impl;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.util.PayUtil;
import com.google.common.collect.Lists;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.jsapi.model.*;

import com.atguigu.tingshu.payment.context.PaymentStrategyContext;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.atguigu.tingshu.payment.strategy.PaymentStrategy;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.model.Transaction;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.util.Map;

@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {

	@Autowired
	private PaymentInfoService paymentInfoService;

	@Autowired
	private RSAAutoCertificateConfig rsaAutoCertificateConfig;

	@Autowired
	private WxPayV3Config wxPayV3Config;
	@Override
	public WeChatPayVo createJsapi(String paymentType, String orderNo) { // 1301-订单 1302-充值
		PaymentStrategy paymentStrategy = PaymentStrategyContext.getpaymentStratege(paymentType);
		return paymentStrategy.createJsapi(paymentType, orderNo);
	}

	@Override
	public boolean queryPayStatus(String orderNo) {
		try {
			//请求微信支付平台的查询支付订单状态
			JsapiServiceExtension serviceExtension = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();
			QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
			request.setOutTradeNo(orderNo);
			request.setMchid(wxPayV3Config.getMerchantId());
			Transaction transaction = serviceExtension.queryOrderByOutTradeNo(request);
			//从transaction获取数据
			if(transaction != null){
				//获取交易状态
				Transaction.TradeStateEnum tradeState = transaction.getTradeState();
				if(Transaction.TradeStateEnum.SUCCESS.equals(tradeState)){//支付成功
					paymentInfoService.updatePaymentStatus(transaction);//更新支付状态
					return true;
				}
			}
		}catch (Exception e){
			e.printStackTrace();
			return false;
		}
		return false;
	}

	@Override
	public void wxnotify(HttpServletRequest request) {
		String wechatSignature = request.getHeader("Wechatpay-Signature");
		String wechatPaySerial = request.getHeader("Wechatpay-Serial");
		String wechatpayNonce = request.getHeader("Wechatpay-Nonce");
		String wechatTimestamp = request.getHeader("Wechatpay-Timestamp");

		String requestBoody = PayUtil.readData(request);
		// 构造 RequestParam
		RequestParam requestParam = new RequestParam.Builder()
				.serialNumber(wechatPaySerial)
				.nonce(wechatpayNonce)
				.signature(wechatSignature)
				.timestamp(wechatTimestamp)
				.body(requestBoody)
				.build();
		// 初始化 NotificationParser
		NotificationParser parser = new NotificationParser(rsaAutoCertificateConfig);
		Transaction transaction = parser.parse(requestParam, Transaction.class);
		if(transaction != null){
			//获取交易状态
			Transaction.TradeStateEnum tradeState = transaction.getTradeState();
			if(Transaction.TradeStateEnum.SUCCESS.equals(tradeState)){ //支付成功
				paymentInfoService.updatePaymentStatus(transaction); //更新支付状态

			}
		}

	}
}
